package net.e_agri.json;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.arnx.jsonic.JSON;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EventConverter extends AbstractContentConverter  implements ContentConverter {

	private static final Log log = LogFactory.getLog(EventConverter.class);

	@Override
	public String parseSource(Source source, String url) {

		List<List<Object>> contentList = new ArrayList<List<Object>>();
		List<String> fieldList = Arrays.asList("title","url","date","body","images");
		
		log.info(url);

		@SuppressWarnings("rawtypes")
		Map<String, List> contentMap = new LinkedHashMap<String, List>();
		contentMap.put("fields", fieldList);
		
		List<Object> record = new ArrayList<Object>();
		Map<String, Object> recordMap = null;
		List<String> imgs = null;
		
		String val = null;
		String field = null;
		for(Element div : source.getAllElements(HTMLElementName.DIV)){
			
			if(!"item".equals(div.getAttributeValue("class")))
				continue;
			
			recordMap = new HashMap<String, Object>();
			record = new ArrayList<Object>();
			imgs = new ArrayList<String>();

			for(Element tr : div.getAllElements(HTMLElementName.TR)){

				for(Element th : tr.getAllElements(HTMLElementName.TH)){
					field = th.getTextExtractor().toString();
				
					if("ニックネーム".equals(field)){
						
						for(Element td : tr.getAllElements(HTMLElementName.TD))
							if(td.getAttributeValue("rowspan") == null)
								//recordMap.put("author", td.getTextExtractor().toString());
								recordMap.put("title", td.getTextExtractor().toString());
						break;
					}else if("作成日時".equals(field)){
						/*
						if(recordMap.containsKey("author"))
							val = recordMap.get("author").toString();
						*/
						recordMap.put("date",
							tr.getFirstElement(HTMLElementName.TD).getTextExtractor().toString());
						
						break;
					}else if("最新タイトル".equals(field)){

						recordMap.put("body",
								tr.getFirstElement(HTMLElementName.TD).getTextExtractor().toString());

						break;
					}else if("最新本文".equals(field)){
						val = tr.getFirstElement(HTMLElementName.TD).getTextExtractor().toString();
						/*
						recordMap.put("body",
								val.substring(0, val.indexOf("もっと読む")));
						*/
						break;
					}
				}

				for(Element td : tr.getAllElements(HTMLElementName.TD)){
					if("photo".equals(td.getAttributeValue("class"))
					&& td.getAttributeValue("rowspan") != null){

						val = td.getFirstElement(HTMLElementName.A).getAttributeValue("href");
						recordMap.put("url", val.replace("./?m=pc&", ""));

						val = td.getFirstElement(HTMLElementName.A)
						.getFirstElement(HTMLElementName.IMG).getAttributeValue("src");
						
						imgs.add(getImageCacheUrl(uriRoot + val.replace("&m=pc", "")));
						recordMap.put("images", imgs);

						break;
					}
				}
			}
			
			for(String fld : fieldList)
				record.add(recordMap.get(fld));
			contentList.add(record);
			log.debug(record.toString());
		}
		contentMap.put("data", contentList);

		return JSON.encode(contentMap);
	}

}
